/**
 * Created by Administrator on 2016/11/11.
 */



// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子：
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}


function dataStr(time){
    var time1 = new Date(time).Format("yyyy-MM-dd");
    return time1;
}

function dataStr2(time){

    var time2 = new Date(time).Format("yyyy-MM-dd hh:mm:ss");
    return time2;
}

function dataStr3(time){

    var time3 = new Date(time).Format("yyyy年MM月dd日");
    return time3;
}


function getValue(el){
    if(el == "" || el == undefined){
        return null;
    }
    return el;
}

var pageInfo = new Object();



function getPageInfo(pageCurrent,pageSize,total){
    pageInfo.pageNo = pageCurrent;
    pageInfo.pageSize = pageSize;
    pageInfo.total = total;
}



function pageTool(pageObj){

    $('#pageTool').empty();

    $('#pageTool').Paging({pagesize:  pageObj.pageSize ,count:  pageObj.total,
        callback:function(page,size,count){
            console.log(arguments);
            /*        alert('当前第 ' +page +'页,每页 '+size+'条,总页数：'+count+'页');*/
            getPageInfo(page,size,pageObj.total);

            sendPost(getQueryParam(pageInfo),false);
        }});
}


/**
 * 返回上一页不刷新
 */
function goBack(){
    history.back();
}








/**
 * 用户修改系列取值方法
 */

function getNationData(){
    post("admin/basicData/getNameTypeByType.do", {type:"民族"}, function (data) {
        console.log(data);
        sys_user_infotVm.dataNation = data;

    }, function (msg) {
        console.log(msg);
    });
}

function getRecordData(){
    post("admin/basicData/getNameTypeByType.do", {type:"学历"}, function (data) {
        console.log(data);
        sys_user_infotVm.dataRecord = data;

    }, function (msg) {
        console.log(msg);
    });
}

function getProvinceData(){
    post("admin/basicData/getNameTypeByType.do", {type:"省"}, function (data) {
        console.log(data);
        sys_user_infotVm.dataProvince = data;

    }, function (msg) {
        console.log(msg);
    });
}



function getWorkCity(id){
    if(id == null || id == undefined || id == ""){
        id = 0;
    }
    post("admin/basicData/getCityNameType.do", {id:id}, function (data) {
        console.log(data);
        sys_user_infotVm.dataWorkCity = data;
    }, function (msg) {
        console.log(msg);
    });
}


function getContactCity(id){
    if(id == null || id == undefined || id == ""){
        id = 0;
    }
    post("admin/basicData/getCityNameType.do", {id:id}, function (data) {
        console.log(data);
        sys_user_infotVm.dataContactCity = data;
    }, function (msg) {
        console.log(msg);
    });
}

function getCity(){
    post("admin/basicData/getNameTypeByType.do", {type:"市"}, function (data) {
        console.log(data);
        sys_user_infotVm.dataWorkCity = data;
        sys_user_infotVm.dataContactCity = data;
    }, function (msg) {
        console.log(msg);
    });



    setTimeout(function () {
        getWorkCity($("#workProvince").val());
        getContactCity($("#contactProvince").val());
    },500);
}


function getUserInfo(){
    var id = api.req.id;
    if(id != null){
        var param = {id:id}

        post("admin/user/getUserInfo.do",param,function (data) {
            console.log(data);
            sys_user_infotVm.data = data;
        },function (msg) {
            console.log(msg);
        });
    }
}


function getRoleData(){
    post("admin/role/getAllRole.do", {}, function (data) {
        console.log(data);
        sys_user_infotVm.dataRole = data;
    }, function (msg) {
        console.log(msg);
    });
}


var sys_user_infotVm = avalon.define({
    $id:"sys_user_info",
    data:[],
    dataNation:[],
    dataRecord:[],
    dataProvince:[],
    dataWorkCity:[],
    dataContactCity:[],
    dataRole:[],
    strTime:function(time){
        return dataStr(time);
    },
    workCityChange:function (e) {
        getWorkCity(e.target.value);
    },
    contactCityChange:function (e) {
        getContactCity(e.target.value);
    }

});


function getUserFormValue(){

    //获得姓名
    var username = $("#username").val();
    //获得性别
    var sex = $("input[name='radio']:checked").val();
    //获得学历
    var baseDataRecord = $("#baseDataRecord").val();
    //获得民族
    var baseDataNation = $("#baseDataNation").val();
    //获得出生日期
    var birthDate = $("#birthDate").val();
    //获得身份证号
    var idcard = $("#idcard").val();
    //获得当前手机号
    var mobile = $("#mobile").val();
    //获得当前邮箱
    var email = $("#email").val();
    //获得工作省份
    var workProvince = $("#workProvince").val();
    //获得工作城市
    var workAddressCityId = $("#workAddressCityId").val();
    //获得工作详细地址
    var workAddress = $("#workAddress").val();
    //获得通讯地址省份
    var contactProvince = $("#contactProvince").val();
    //获得通讯地址城市
    var addressCityId = $("#addressCityId").val();
    //获得通讯详细地址
    var address = $("#address").val();
    //获得角色id
    var strRoleIds = $("#strRoleIds").val();

    var workAddressDetail = (workProvince + "-" + workAddressCityId + "-" + workAddress);
    var addressDetail = (contactProvince + "-" + addressCityId + "-" + address);


    userParam.id = sys_user_infotVm.data.id;
    userParam.username = username;
    userParam.sex = sex;
    userParam.baseRecordId = baseDataRecord;
    userParam.baseNationId = baseDataNation;
    userParam.birthDate = birthDate;
    userParam.idcard = idcard;
    userParam.mobile = mobile;
    userParam.email = email;
    userParam.workAddress = workAddressDetail;
    userParam.address = addressDetail;

    if(strRoleIds != null && strRoleIds != undefined){
        userParam.strRoleIds = strRoleIds;
    }

}

var userParam = {
    id:null,
    username:null,
    sex:null,
    baseRecordId:null,
    baseNationId:null,
    birthDate:null,
    idcard:null,
    mobile:null,
    email:null,
    workAddress:null,
    address:null,
    strRoleIds:null
}




function saveUserInfo(){
    getUserFormValue();
    post("admin/user/getUserInfo.do",userParam,function (data) {
        console.log(data);
    },function (msg) {
        console.log(msg);
    });
}



